<?php
/*
*
* Description:	Used with the Photo Concept Theme to add an interface to Flickr.
*				File to generate functions for Flickr calls
*/

function prose_flickr_url_build(){
	$params = array('api_key'	=> $prose_flickr_api,
			'api_secret'	=> $prose_flickr_secret,
			'method'	=> 'flickr.photos.getInfo',
			'permissions'	=> 'read',
			'format'	=> 'php_serial');
	
	$encoded_params = array();
	
	foreach($params as $k => $v){
		$encoded_params[] = urlencode($k).'='.urlencode($v);
	}
	
	
	#
	# call the API and decode the response
	#
	
	$url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);
	
	$rsp = file_get_contents($url);
	
	$rsp_obj = unserialize($rsp);
	
	
	#
	# display the photo title (or an error if it failed)
	#
	
	if ($rsp_obj['stat'] == 'ok'){
		$photo_title = $rsp_obj['photo']['title']['_content'];
		echo "Title is $photo_title!";
	}else{
		echo "Call failed!";
	}
}

function prose_flickr_get_user_id_email($prose_flickr_api, $prose_flickr_secret, $prose_username){
	$params = array('api_key'	=> $prose_flickr_api,
			'api_secret'	=> $prose_flickr_secret,
			'username'	=> $prose_username,
			'method'	=> 'flickr.people.findByEmail',
			'permissions'	=> 'read',
			'format'	=> 'php_serial');
	
	$encoded_params = array();
	
	foreach($params as $k => $v){
		$encoded_params[] = urlencode($k).'='.urlencode($v);
	}
	
	#
	# call the API and decode the response
	#
	
	$url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);
	$rsp = file_get_contents($url);
	$rsp_obj = unserialize($rsp);

	#
	# display the user id(or an error if it failed)
	#
	
	if ($rsp_obj['stat'] == 'ok'){
		$prose_flickr_user_id = $rsp_obj['user']['id'];
		
		return $prose_flickr_user_id;
	}else{
		echo "Call failed!";
 	}
}

function prose_flickr_get_user_id_username($prose_flickr_api, $prose_flickr_secret, $prose_username){
	$params = array('api_key'	=> $prose_flickr_api,
			'api_secret'	=> $prose_flickr_secret,
			'username'	=> $prose_username,
			'method'	=> 'flickr.people.findByUsername',
			'permissions'	=> 'read',
			'format'	=> 'php_serial');
	
	$encoded_params = array();
	
	foreach($params as $k => $v){
		$encoded_params[] = urlencode($k).'='.urlencode($v);
	}
	
	#
	# call the API and decode the response
	#
	
	$url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);
	$rsp = file_get_contents($url);
	$rsp_obj = unserialize($rsp);

	#
	# display the user id(or an error if it failed)
	#
	
	if ($rsp_obj['stat'] == 'ok'){
		$prose_flickr_user_id = $rsp_obj['user']['id'];
		
		return $prose_flickr_user_id;
	}else{
		echo "Missing a username..";
 	}
}

function prose_flickr_get_user_photosets($prose_flickr_api, $prose_flickr_user_id){
	$params = array('api_key'	=> $prose_flickr_api,
			'user_id'	=> $prose_flickr_user_id,
			'method'	=> 'flickr.photosets.getList',
			'permissions'	=> 'read',
			'format'	=> 'php_serial');
	
	$encoded_params = array();
	
	foreach($params as $k => $v){
		$encoded_params[] = urlencode($k).'='.urlencode($v);
	}
	
	#
	# call the API and decode the response
	#
	
	$url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);
	$rsp = file_get_contents($url);
	$rsp_obj = unserialize($rsp);

	#
	# display the user id(or an error if it failed)
	#
	
	if ($rsp_obj['stat'] == 'ok'){
		$flickr_photoset_information = array();
		
		$flickr_set_count = $rsp_obj['photosets']['total'];
		foreach($rsp_obj['photosets']['photoset'] as $photoset){
			$flickr_photoset_information[] = array( 'title'		=> $photoset['title']['_content'],
								'description'	=> $photoset['description']['_content'],
								'id'		=> $photoset['id']);
		}
		return($flickr_photoset_information);
	}else{
		echo "Please supply a username";
	}
}

function prose_flickr_get_photoset_info($prose_flickr_api, $prose_flickr_set){
	$params = array('api_key'	=> $prose_flickr_api,
			'photoset_id'	=> $prose_flickr_set,
			'method'	=> 'flickr.photosets.getInfo',
			'permissions'	=> 'read',
			'format'	=> 'php_serial');
	
	$encoded_params = array();
	
	foreach($params as $k => $v){
		$encoded_params[] = urlencode($k).'='.urlencode($v);
	}
	
	// call the API and decode the response
	
	$url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);
	$rsp = file_get_contents($url);
	$rsp_obj = unserialize($rsp);

	#
	# display the user id(or an error if it failed)
	#
	
	if ($rsp_obj['stat'] == 'ok'){
		$flickr_photoset_information = array( 'title'		=> $rsp_obj['photoset']['title']['_content'],
						      'description'	=> $rsp_obj['photoset']['description']['_content']);
		
		return($flickr_photoset_information);
	}else{
		echo "Missing information..";
	}
}

function prose_flickr_get_photos($prose_flickr_api, $prose_flickr_set){
	$params = array('api_key'	=> $prose_flickr_api,
			'photoset_id'	=> $prose_flickr_set,
			'method'	=> 'flickr.photosets.getPhotos',
			'permissions'	=> 'read',
			'format'	=> 'php_serial');
	
	$encoded_params = array();
	
	foreach($params as $k => $v){
		$encoded_params[] = urlencode($k).'='.urlencode($v);
	}
	
	#
	# call the API and decode the response
	#
	
	$url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);
	$rsp = file_get_contents($url);
	$rsp_obj = unserialize($rsp);

	#
	# display the user id(or an error if it failed)
	#
	
	if ($rsp_obj['stat'] == 'ok'){
		$flickr_photoset_photos = array();
		
//		$flickr_set_count = $rsp_obj['photoset']['photo'];
		foreach($rsp_obj['photoset']['photo'] as $photo){
			$flickr_photoset_photos[] = array('id'		=> $photo['id'],
							  'secret'	=> $photo['secret'],
							  'server'	=> $photo['server'],
							  'farm'	=> $photo['farm'],
							  'title' 	=> $photo['title']);
		}

		return($flickr_photoset_photos);
	}else{
		echo "Call failed!";
	}
}

function prose_flickr_generate_photo_url($flickr_photoset_photos){
	$flickr_photoset_photo_urls = array();
	
	foreach($flickr_photoset_photos as $photo_url){
		$flickr_photoset_photo_urls[] = "farm" . $photo_url['farm'] . ".staticflickr.com/" . $photo_url['server'] . "/" .$photo_url['id'] . "_" . $photo_url['secret'] . "_m.jpg " ;
	}

	return($flickr_photoset_photo_urls);	
}

/*
// Functions to operate the user authentication
function prose_flickr_settings_api(){
	add_settings_section(	$id,
				'prose_photo_album_location',
				$callback,
				$flickr_photos
			    );
	
	add_settings_field( $flickr_api_id, 
			    $flickr_user, 
			    'get_flickr_user_name', 
			    'flickr_photos',
			    $section = 'flickr-photos'
			  );
	register_setting( $option_group, $option_name, $sanitize_callback );
}
*/

/*
*  Custom Post type for the Flickr Posts
*  Will need ...
*/
?>